<?php

namespace App\Models\Shop;

use Illuminate\Database\Eloquent\Model;
use Qiniu\Http\Request;

class Customer extends Model
{
    protected $guarded = ['language','country','privilege'];

    static function customers_all($request){
        /***
         * 多条件搜索
         */
        $where = function ($query) use ($request) {
            //按会员昵称
            if ($request->has('nickname') and $request->nickname != '') {
                $search = "%" . $request->nickname . "%";
                $query->where('nickname', 'like', $search);
            }
            //按openid
            if ($request->has('openid') and $request->openid != '') {
                $search = "%" . $request->openid . "%";
                $query->where('openid', 'like', $search);
            }
            //按性别
            if ($request->has('sex') and $request->sex != '-1'){
                $sex = $request->sex;
                $query->where('sex',$sex);
            }
            //按注册时间
            if ($request->has('created_at') and $request->created_at != '') {
                $time = explode(" ~ ", $request->input('created_at'));
                $start = $time[0] . ' 00:00:00';
                $end = $time[1] . ' 23:59:59';
                $query->whereBetween('created_at', [$start, $end]);
            }
        };
        $customers = Customer::orderBy('id', 'desc')->where($where)->paginate(10);
        return $customers;
    }

    public function orders()
    {
        return $this->hasMany('App\Models\Shop\Order');
    }
}
